unit Cad_Contratante;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Formulario_Padrao, ExtCtrls, ComCtrls, StdCtrls, Buttons, ToolEdit, Mask,
  CurrEdit, RDprint, TFlatButtonUnit;

type
  TFrmCad_Contratante = class(Tfrm_padr)
    Label1: TLabel;
    ed_cgc: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    ed_razao: TEdit;
    ed_dias_aviso: TCurrencyEdit;
    ed_fixo: TCurrencyEdit;
    ed_mes: TCurrencyEdit;
    ed_data: TDateEdit;
    procedure ed_cgcKeyPress(Sender: TObject; var Key: Char);
    procedure FormActivate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure btnCancelarClick(Sender: TObject);
    procedure btnGravarClick(Sender: TObject);
    procedure ed_cgcKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmCad_Contratante: TFrmCad_Contratante;

implementation

uses clsContratante, biblio, clsSenha;

var
  contratante: TContratante;

{$R *.DFM}

procedure TFrmCad_Contratante.ed_cgcKeyPress(Sender: TObject; var Key: Char);
begin
  inherited;
  if not (Key in ['0'..'9', Chr(VK_DELETE), Chr(VK_BACK)]) then Key := #0;
end;

procedure TFrmCad_Contratante.FormActivate(Sender: TObject);
begin
  inherited;
  ed_cgc.text := '';
  ed_razao.text := '';
  ed_dias_aviso.text := '';
  ed_fixo.text := '';
  ed_mes.text := '1';
  ed_data.date := date;
  ed_cgc.setfocus;
end;

procedure TFrmCad_Contratante.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  inherited;
  contratante := TContratante.Create;
  if not contratante.Verifica_Existe then canclose := false else canclose := true;
  contratante.free;
end;

procedure TFrmCad_Contratante.btnCancelarClick(Sender: TObject);
begin
  inherited;
  FormActivate(sender);
end;

procedure TFrmCad_Contratante.btnGravarClick(Sender: TObject);
var
  Data: TDateTime;
begin
  inherited;
  contratante := TContratante.Create;
  contratante.razao := ed_razao.text;
  contratante.dias_aviso := strzero(ed_dias_aviso.text, 2);
  contratante.dia_fixo := strzero(ed_fixo.text, 2);
  contratante.meses := strzero(ed_mes.text, 2);
  contratante.data_exp := ed_data.date;
  Data := ed_data.date;
  contratante.checkSum := ((Data + 13) * 2) - 13;
  contratante.Gravar(ed_cgc.text);
  contratante.free;
end;

procedure TFrmCad_Contratante.ed_cgcKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  inherited;
  if key = 12 then ed_cgc.text := strzero(ed_cgc.text, 14);
end;

end.
